﻿/*!
 *
 * Date: 10/05/2011
 * jQ-comerce javaScript plugin 
 * http://pukis.com.br/
 *
 * Copyright 2011, Felipe Assunção //felipeilllustrator@gmail.com //@felipeuntill
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 *
 * Require jQuery JavaScript Library v1.4 or Newest
 * http://jquery.com/
 * Copyright 2011, John Resig
 * Released under the MIT and GPL Licenses.
 * 
 */
$(function() {
    // Begging the plugin resposable of make the select UI.
    $.fn.jQSelect = (function(options) {
        
		// Defining all customs variables.
        var options = $.extend({
            selectInstance : $(this),
            optionList : $(this).find("option"),
            uiContainer : null,
            uiChild : null,
            uiItems : null
        },options);
    
        // Creating UI the element.
        $(this).before('<div class="jQcustomselect" ><a title="Clique aqui para selecionar uma op&ccedil;&atilde;o" class="jqselectlabel" href="javascript:void(0)">Selecione</a></div>');
        // Matching the added object.
        options.uiContainer = $(this).prev();
        // Creating UI inside the element.
        $(options.uiContainer).append('<ul class="ui jQSelect" > </ul>')
        // Matching the added object.
        options.uiChild = $(options.uiContainer).find(".jQSelect");
        
        // Travelsing all option's.
        $(options.optionList).each(function() {
            // Appending the option content on a ul created previously.
            $(options.uiChild).append('<li><a href="javascript:void(0)"> ' + $(this).html() + '</a></li>');
        });
        
        // Matching the added items.
        options.uiItems = $(options.uiContainer).find("li");
        
        // When the list item has clicked.
        $(options.uiItems).click(function() {
            // Setting up the value to the original select element.
            $(options.selectInstance).val($(options.optionList[ $(this).index($(options.optionList).selection) ]).val());
			// Setting up the select texto to the label.
			$(options.uiContainer).children('.jqselectlabel').html($(options.optionList[ $(this).index($(options.optionList).selection) ]).html());
			// hiding the list.
            $(options.uiChild).hide();
			$(options.selectInstance).trigger("change");
        });
        
		// When then label has clicked.
        $(options.uiContainer).children('.jqselectlabel').click(function() {
			// Listing the select options.
            $(options.uiChild).slideToggle();
        
        });
		
		$("#espec_0_opcao_0").trigger("change");
    
    });

    // Begging the plugin resposable of make the select UI.
    $.fn.jQSelectToList = (function(options) {
        
		// Defining all customs variables.
        var options = $.extend({
            selectInstance : $(this),
            optionList : $(this).find("option"),
			uiItems : 'null',
			uiContainer : 'null',
			backgroundUri : 'http://webstore.reference.vtexlab.com.br/Control/ArquivoExibir.aspx?NomeArquivo=',
			selectfunction : (function(matched) {


			})
        },options);
	
		function replaceAll(string, token, newtoken) {
			while (string.indexOf(token) != -1) {
				string = string.replace(token, newtoken);
			}
			return string;
		}
	
				
        // Creating UI the element.
        $(this).before('<ul class="jQSelectToList clearfix"> </ul>');
        // Matching the added object.
        options.uiContainer = $(this).prev();
    
        // Travelsing all option's.
        $(options.optionList).each(function() {
			if( $(this).val() != null && $(this).val() != '' && $(this).val() != ' ' ) {
				// Appending the option content on a ul created previously.
				// Appending the option content on a ul created previously.
				var  src = $(this).html();
				var  src = replaceAll(src, " ", "_");
				var  src = replaceAll(src, "(", "-");
				var  src = replaceAll(src, ")", "-");
				$(options.uiContainer).append('<li style="background : url(' + options.backgroundUri + src + ')"><a href="javascript:void(0)" title="' + $(this).html() + '" rel="' + $(this).html() + '"><span>' + $(this).html() + '</span></a></li>');
			}
	    });
        
        // Matching the added items.
        options.uiItems = $(options.uiContainer).find('li');
      
		$(options.selectInstance).val($(options.uiItems[1]).find('span').html());
		$( options.uiItems[0]).addClass('active');
		
        // When the list item has clicked.
        $(options.uiItems).click(function() {
            
			$(options.uiItems).removeClass('active');
			$(this).addClass('active');

			$(options.selectInstance).val($(this).find('span').html());
			
			if( typeof( options.selectfunction ) === 'function' ) {
				//Calling the function
				options.selectfunction.call("red");
			}
			
			$(options.selectInstance).trigger("change");
		
        });
		
		$(options.selectInstance).trigger("change");
    
    });
	
	// Begging the plugin responsable of improve menu size limit.
    $.fn.jQMaxmenu = (function(options) {
        // Defining all customs variables.
        var options = $.extend({
            selectInstance : $(this),
			menuLimit : 5,
			expandText : "Expandir",
			hideText : "Minimizar"
        },options);
		
		// Travelsing all lists.
		$(this).each(function() {
			// If the matched list contains more of intens quantity.
			if( $(this).children('li').size() > options.menuLimit ) {
				// Appeding the html used to build the effect.
				$(this).append('<div class="jQBoxcontainer"><div class="jQboxlist"> </div><a class="jQboxButton" href="javascript:void(0)">' + options.expandText + '</a></div>')
				// Adding all others list items on the div previously added.
				$(this).find('.jQboxlist').append($(this).children('li:gt(' + ( options.menuLimit - 1 ) + ')')).hide();
			} 
			
		});
		
		// When the button expand has clicked.
		$(this).find('.jQboxButton').click(function() {
		
			// Verifying if the content has alredy opened.
			if( $(this).prev().is('.jQboxlist:hidden') ) {
				// Opening the content.
				$(this).prev().slideDown();
				// Changing the displayed text.
				$(this).html(options.hideText);
			// else the content has opened then hide.
			} else {
				// Hidding the content.
				$(this).prev().slideUp();
				// Changing the displayed text.
				$(this).html(options.expandText);
			}
		
		});
		
    });
	
	// Begin the plugin resposable of move the product list when the limit has passed.
	$.fn.DasluSlider = (function (options) {
		// Defining all customs variables.
        var options = $.extend({
            selectInstance : $(this),
            selectList : $(this).find('li:has("img,a,div,swf,embed,script")'),
            selectListSize : $(this).find('li:has("img,a,div,swf,embed,script")').size(),
			containerInstance : null,
			containerLimit : 1,
			moveSize : $(window).width(),
			prevButton :  null,
			nextButton :  null,
			track : null,
			enableKeyboard: false
	    },options);

		$(options.selectList).width(options.moveSize);
		$(options.selectList).find('a').css({ 'width' : '100%' });
		
		// When the image width is > of the resolution.
		if ( $(options.selectList).find('img,object').width() > options.moveSize ) {
			$(options.selectList).find('img,object').css( {  'margin-left' : '-' + ( ( $(options.selectList).find('img,object').width() - options.moveSize )  / 2 ) + 'px'  } );
		// When the image width is < of the resolution.
		} else {			
			$(options.selectList).find('img,object').css({ 'display' :  'block' , 'margin' : '0  auto' });
		}
		
		// When the window has resized.
		$(window).resize(function() {
		
			options.moveSize = $(window).width();
			$(options.selectList).width(options.moveSize);
			$(options.selectList).find('a').css({ 'width' : '100%' });
			
			// When the image width is > of the resolution.
			if ( $(options.selectList).find('img,object').width() > options.moveSize ) {
				$(options.selectList).find('img,object').css( {  'margin-left' : '-' + ( ( $(options.selectList).find('img,object').width() - options.moveSize )  / 2 ) + 'px'  } );
			// When the image width is < of the resolution.
			} else {			
				$(options.selectList).find('img,object').css({ 'display' :  'block' , 'margin' : '0  auto' });
			}
			
		});
		
		// Detecting a window change.
		var detectLoop = window.setInterval(resizeSlider, 2000);
			
		// When the window has resized.
		function resizeSlider () {
		
			options.moveSize = $(window).width();
			$(options.selectList).width(options.moveSize);
			$(options.selectList).find('a').css({ 'width' : '100%' });
			
			// When the image width is > of the resolution.
			if ( $(options.selectList).find('img,object').width() > options.moveSize ) {
				$(options.selectList).find('img,object').css( {  'margin-left' : '-' + ( ( $(options.selectList).find('img,object').width() - options.moveSize )  / 2 ) + 'px'  } );
			// When the image width is < of the resolution.
			} else {			
				$(options.selectList).find('img,object').css({ 'display' :  'block' , 'margin' : '0  auto' });
			}
			
		}
		
		$(this).find('li:not(:has("img,a,div,swf,embed,script"))').remove();
		
		options.track = 0;
		var boxindex = 0;
		
		// appending a container to the mathed ul.
		$(options.selectInstance).before('<div id="JQRollInstance"> </div>');
		// Matching the added container to a variable.
		options.containerInstance = $(options.selectInstance).prev();
		// Appending the mathed ul on the container.
		$(options.containerInstance).append(options.selectInstance);
		// Appeding the previously button.
		$(options.containerInstance).before('<a class="JQPrevButton" href="javascript:void(0)">Prev</a>');
		// Appeding the next button
		$(options.containerInstance).after('<a class="JQNextButton disabled" href="javascript:void(0)">Next</a>');
		// Finding the prev button.
		options.prevButton = $(options.containerInstance).parent().find(".JQPrevButton");
		// Finding the next button.
		options.nextButton = $(options.containerInstance).parent().find(".JQNextButton");
		
		if ( options.selectListSize <= options.containerLimit )
		{
		
			$(options.prevButton).addClass("disabled");
		
		}

		// When the prev buttton has clicked.
		$(options.prevButton).click(function () {
			
			if( ( boxindex + options.containerLimit ) + 1 <= options.selectListSize ) {
			
				options.track -= options.moveSize;
				boxindex += 1;
				
				
				$(options.selectInstance).animate({ left : options.track }, 500);
				
				if( ( boxindex + options.containerLimit ) + 1 <= options.selectListSize ) {
					
					$(options.prevButton).removeClass('disabled');
					
				}else{
					
					$(options.prevButton).addClass('disabled');
				
				}
				
				if( options.track < 0 ) {
				
					$(options.nextButton).removeClass('disabled');
					
				}else{
					
					$(options.nextButton).addClass('disabled');
				
				}
				
			}
			
		});
		
		// When the next button has clicked.
		$(options.nextButton).click(function () {
		
			//if( options.track < ( options.moveSize * options.selectListSize )  && options.track < ( options.moveSize * options.containerLimit )  ) {
			if( options.track < 0 ) {
				
				options.track += options.moveSize;
				boxindex -= 1;
				
				$(options.selectInstance).animate({ left : options.track }, 500);
			
				if( options.track < 0 ) {
				
					$(options.nextButton).removeClass('disabled');
				
				}else{
					
					$(options.nextButton).addClass('disabled');
					
				}
				
				if( ( boxindex + options.containerLimit ) + 1 <= options.selectListSize ) {
				
					$(options.prevButton).removeClass('disabled');
					
				}else{
					
					$(options.prevButton).addClass('disabled');
				
				}
				
			}

		
		});
		
		$(document).keydown(function (event) {
			
			if ( options.enableKeyboard ) {
			
				// handle cursor keys
				if (event.keyCode == 39) {
					// go left
					if( ( boxindex + options.containerLimit ) + 1 <= options.selectListSize ) {
					
						options.track -= options.moveSize;
						boxindex += 1;
						
						
						$(options.selectInstance).animate({ left : options.track }, 500);
						
						if( ( boxindex + options.containerLimit ) + 1 <= options.selectListSize ) {
							
							$(options.prevButton).removeClass('disabled');
							
						}else{
							
							$(options.prevButton).addClass('disabled');
						
						}
						
						if( options.track < 0 ) {
						
							$(options.nextButton).removeClass('disabled');
							
						}else{
							
							$(options.nextButton).addClass('disabled');
						
						}
						
					}
					
				} else if (event.keyCode == 37) {
					// go right
					
					if( options.track < 0 ) {
						
						options.track += options.moveSize;
						boxindex -= 1;
						
						$(options.selectInstance).animate({ left : options.track }, 500);
					
						if( options.track < 0 ) {
						
							$(options.nextButton).removeClass('disabled');
						
						}else{
							
							$(options.nextButton).addClass('disabled');
							
						}
						
						if( ( boxindex + options.containerLimit ) + 1 <= options.selectListSize ) {
						
							$(options.prevButton).removeClass('disabled');
							
						}else{
							
							$(options.prevButton).addClass('disabled');
						
						}
						
					}
					
				}
				
			}
			
		});
		
	});
	
	// Begin the plugin resposable of move the product list when the limit has passed.
	$.fn.jQRoll = (function (options) {
		// Defining all customs variables.
        var options = $.extend({
            selectInstance : $(this),
            selectList : $(this).find('li:has("img,a,div,swf,embed,script")'),
            selectListSize : $(this).find('li:has("img,a,div,swf,embed,script")').size(),
			containerInstance : null,
			containerLimit : 1,
			moveSize : $(this).find('li:first-child').width(),
			prevButton :  null,
			nextButton :  null,
			track : null,
			enableKeyboard: false
	    },options);

		$(this).find('li:not(:has("img,a,div,swf,embed,script"))').remove();
		
		options.track = 0;
		var boxindex = 0;
		
		// appending a container to the mathed ul.
		$(options.selectInstance).before('<div id="JQRollInstance"> </div>');
		// Matching the added container to a variable.
		options.containerInstance = $(options.selectInstance).prev();
		// Appending the mathed ul on the container.
		$(options.containerInstance).append(options.selectInstance);
		// Appeding the previously button.
		$(options.containerInstance).before('<a class="JQPrevButton" href="javascript:void(0)">Prev</a>');
		// Appeding the next button
		$(options.containerInstance).after('<a class="JQNextButton disabled" href="javascript:void(0)">Next</a>');
		// Finding the prev button.
		options.prevButton = $(options.containerInstance).parent().find(".JQPrevButton");
		// Finding the next button.
		options.nextButton = $(options.containerInstance).parent().find(".JQNextButton");
		
		if ( options.selectListSize <= options.containerLimit )
		{
		
			$(options.prevButton).addClass("disabled");
		
		}

		// When the prev buttton has clicked.
		$(options.prevButton).click(function () {
			
			if( ( boxindex + options.containerLimit ) + 1 <= options.selectListSize ) {
			
				options.track -= options.moveSize;
				boxindex += 1;
				
				
				$(options.selectInstance).animate({ left : options.track }, 500);
				
				if( ( boxindex + options.containerLimit ) + 1 <= options.selectListSize ) {
					
					$(options.prevButton).removeClass('disabled');
					
				}else{
					
					$(options.prevButton).addClass('disabled');
				
				}
				
				if( options.track < 0 ) {
				
					$(options.nextButton).removeClass('disabled');
					
				}else{
					
					$(options.nextButton).addClass('disabled');
				
				}
				
			}
			
		});
		
		// When the next button has clicked.
		$(options.nextButton).click(function () {
		
			//if( options.track < ( options.moveSize * options.selectListSize )  && options.track < ( options.moveSize * options.containerLimit )  ) {
			if( options.track < 0 ) {
				
				options.track += options.moveSize;
				boxindex -= 1;
				
				$(options.selectInstance).animate({ left : options.track }, 500);
			
				if( options.track < 0 ) {
				
					$(options.nextButton).removeClass('disabled');
				
				}else{
					
					$(options.nextButton).addClass('disabled');
					
				}
				
				if( ( boxindex + options.containerLimit ) + 1 <= options.selectListSize ) {
				
					$(options.prevButton).removeClass('disabled');
					
				}else{
					
					$(options.prevButton).addClass('disabled');
				
				}
				
			}

		
		});
		
		$(document).keydown(function (event) {
			
			if ( options.enableKeyboard ) {
			
				// handle cursor keys
				if (event.keyCode == 39) {
					// go left
					if( ( boxindex + options.containerLimit ) + 1 <= options.selectListSize ) {
					
						options.track -= options.moveSize;
						boxindex += 1;
						
						
						$(options.selectInstance).animate({ left : options.track }, 500);
						
						if( ( boxindex + options.containerLimit ) + 1 <= options.selectListSize ) {
							
							$(options.prevButton).removeClass('disabled');
							
						}else{
							
							$(options.prevButton).addClass('disabled');
						
						}
						
						if( options.track < 0 ) {
						
							$(options.nextButton).removeClass('disabled');
							
						}else{
							
							$(options.nextButton).addClass('disabled');
						
						}
						
					}
					
				} else if (event.keyCode == 37) {
					// go right
					
					if( options.track < 0 ) {
						
						options.track += options.moveSize;
						boxindex -= 1;
						
						$(options.selectInstance).animate({ left : options.track }, 500);
					
						if( options.track < 0 ) {
						
							$(options.nextButton).removeClass('disabled');
						
						}else{
							
							$(options.nextButton).addClass('disabled');
							
						}
						
						if( ( boxindex + options.containerLimit ) + 1 <= options.selectListSize ) {
						
							$(options.prevButton).removeClass('disabled');
							
						} else {
							
							$(options.prevButton).addClass('disabled');
						
						}
						
					}
					
				}
				
			}
			
		});
		
	});
	
	// Begin the plugin resposable of move the product list when the limit has passed.
	$.fn.DasluProductSlider = (function (options) {
		
		$(this).find('li:gt(5)').remove();
	
		// Defining all customs variables.
        var options = $.extend({
            selectInstance : $(this),
            selectList : $(this).find('li'),
            selectListSize : $(this).find('li').size(),
			containerInstance : null,
			prevButton :  null,
			nextButton :  null,
			track : null,
			enableKeyboard: false
	    },options);
		
 		// appending a container to the mathed ul.
		$(options.selectInstance).before('<div id="JQRollInstance"> </div>');
		// Matching the added container to a variable.
		options.containerInstance = $(options.selectInstance).prev();
		// Appending the mathed ul on the container.
		$(options.containerInstance).append(options.selectInstance);
		// Appeding the previously button.
		$(options.containerInstance).before('<a class="JQPrevButton disabled" href="javascript:void(0)">Prev</a>');
		// Appeding the next button
		$(options.containerInstance).after('<a class="JQNextButton" href="javascript:void(0)">Next</a>');
		// Finding the prev button.
		options.prevButton = $(options.containerInstance).parent().find(".JQPrevButton");
		// Finding the next button.
		options.nextButton = $(options.containerInstance).parent().find(".JQNextButton");

		if ( options.selectListSize < 5 ) {
			$(options.prevButton).addClass('disabled');
			$(options.nextButton).addClass('disabled');
		} 
		
		$(options.selectInstance).css({ 'left' : '0px' });

		// When the prev buttton has clicked.
		$(options.prevButton).click(function () {

			switch(options.selectListSize) {
				case 5:
					$(options.selectInstance).animate({ left : 0 }, 500);
					$(options.prevButton).addClass('disabled');
					$(options.nextButton).removeClass('disabled');
					break;
				case 6:
					$(options.selectInstance).animate({ left : 0 }, 500);
					$(options.prevButton).addClass('disabled');
					$(options.nextButton).removeClass('disabled');
					break;	
				default:
					$(options.selectInstance).animate({ left : 0 }, 500);
					$(options.prevButton).addClass('disabled');
					$(options.nextButton).addClass('disabled');
				break;
			}
			
		});
		
		// When the next button has clicked.
		$(options.nextButton).click(function () {
			switch(options.selectListSize) {
				case 5:
					$(options.selectInstance).animate({ left : -120 }, 500);
					$(options.nextButton).addClass('disabled');
					$(options.prevButton).removeClass('disabled');
					break;
				case 6:
					$(options.selectInstance).animate({ left : -240 }, 500);
					$(options.nextButton).addClass('disabled');
					$(options.prevButton).removeClass('disabled');
					break;	
				default:
					$(options.selectInstance).animate({ left : 0 }, 500);
					$(options.prevButton).addClass('disabled');
					$(options.nextButton).addClass('disabled');
				break;
			}
			
		});
		
	});
	
	$.fn.VerticalSlide = (function (options,diference) {
    
        var options = $.extend({
        
            
            boxes : $(this).find('li').size(),
            boxesHeight : $(this).find('li').height() + diference,
            containerSupport : 5,
            containerSize : $(this).find('li').size() * $(this).find('li').height() + 150,
            prev : $('.topcontrols .controlup'),
            next : $('.bottomcontrols .controldown'),
            track: $(this)
       
        
        },options);

        
        //Verifying if the page want the plugin
        if ( options.boxes <= options.containerSupport ) return false; 
        
        
        // Variable resposable of alocate the slide index
        var boxIndex = 1
        var track = 0
        
        // Fixing the ul width;
        $(options.track).height(options.containerSize);
        
        // Function resposable of move the roll to the next step.
        function gotoNext() {
        
            if ( ( boxIndex + options.containerSupport ) <= options.boxes  ) {
                
                track -=  options.boxesHeight;
                
                // Decreasing the top anchor of the roll
                $(options.track).animate({ top :  track }, 500);
                // Increasing the index
                boxIndex++;
            
            }
        
        }
        
        // Function resposable of move the roll to the previously step.
        function gotoPrev() {
            
            if ( boxIndex - 1 > 0 ) {
              
                track +=  options.boxesHeight;
                // Decreasing the left anchor of the roll
                $(options.track).animate({ top : track }, 500);
                // Increasing the index
                boxIndex--;
            
            }
            
        }

        // Prev Button Click Event
        $(options.prev).click(function() {
            gotoPrev();
       
        });
        
        // Next Button click Event
        $(options.next).click(function() {
            gotoNext();
        
        });
    
    });
	
});